package com.sc.web.gwt.server.util;

import com.sc.web.gwt.server.persistence.UserAccountPersistence;
import com.sc.web.gwt.shared.model.UserAccount;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Duong
 * Date: 8/8/11
 * Time: 6:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserAccountGenerator {
    public static String getUserAccount(String firstName, String lastName) {
        UserAccountPersistence accountPersistence = new UserAccountPersistence();
        List<UserAccount> currentUserAccounts = new ArrayList<UserAccount>();
        currentUserAccounts = accountPersistence.getUserAccountDataByFirstAndLastName(firstName, lastName);
        int i = 0;
        if (currentUserAccounts != null) {
            i = currentUserAccounts.size() + 1;
        }
        if (i > 0) {
            return generateUserAccountName(firstName, lastName) + i;
        }
        return generateUserAccountName(firstName, lastName);
    }

    public static String generateUserAccountName(String firstName, String lastName) {
        String userAccountName = firstName;

        if (lastName != null && !lastName.equals("")) {
            String[] lastNames = lastName.split(" ");
            for (int i = 0; i < lastNames.length; i++) {
                if (lastNames[i] != null && !lastNames[i].equals("")) {
                    userAccountName += lastNames[i].charAt(0);
                }
            }

            return userAccountName;
        }

        return userAccountName;
    }
}
